﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceApplication.Service;
using ServiceApplication.Service.Basic;
using System.Data;
using CommonLibrary.Common;

namespace AspWebTest
{
    public partial class Test : System.Web.UI.Page
    {
        FormService formService = new FormService();
        BasicService basicService = new BasicService();
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write("");
            //string s = " <input id=\"text1\" runat=\"server\" name=\"text1\" type=\"text\" style=\"left:40px; top:70px;width:40px;height:40px; position: absolute; \"  />";
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "js", " <input id=\"text1\" runat=\"server\" name=\"text1\" type=\"text\" style=\"left:40px; top:70px;width:40px;height:40px; position: absolute; \"  />");
            //document.getElementById("DIV_Itrm").innerHTML = s;
            InitTree();
            TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
        }

        void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode node = TreeView1.SelectedNode;
            string text = node.Text;
            string id = node.Value;

            //string sIndex = TreeView1;
            //if (sIndex.IndexOf(".") >= 0)
            //{
            //    sIndex = sIndex.Substring(0, sIndex.LastIndexOf("."));
            //}

            TreeView1.Visible = false;
        }

        private void InitTree()
        {
            //TreeView1.Attributes["onclick"] = "javascript: if(this.clickedNodeIndex != null)this.queueEvent('onselectednodechanged', '' + ',' + this.clickedNodeIndex)"; 
          
            TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
            //使不闪
            //string strTreeName = "TreeView1 ";
            //string strRef = Page.GetPostBackEventReference(TreeView1);
            //string strScript = @" <script     language=\'JavaScript\'>     \n " +
            //    " <!--     \n " +
            //    "   function     initTree()     {     \n " +
            //    strTreeName +
            //    ".onSelectedIndexChange     =     function()     {     \n " +
            //    "if     (event.oldTreeNodeIndex     !=         event.newTreeNodeIndex)     \n " +
            //    "this.queueEvent( 'onselectedindexchange ',     event.oldTreeNodeIndex     +     ', '     +     event.newTreeNodeIndex);     \n " +
            //    "window.setTimeout( ' " + strRef.Replace(" ' ", "\\ ' ") + " ',     0,     'JavaScript ');     \n " +
            //    "  }     \n " + "                         }     \n " + "//     -->     \n " + " </script> ";
            //Page.RegisterClientScriptBlock("InitTree ", strScript);


            DataTable dt = basicService.QueryAllDepts();
            WebNodeHelp nh = new WebNodeHelp(dt, "部门编码", "部门名称", "上级编码", "部门编码=1");

            //将子node写入treeview
            foreach (var item in basicService.GetWebUserNodeList())
            {
                TreeView1.Nodes.Add(item);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TreeView1.Visible = false;
        }

    }
}
